﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Linq;
using System.Web.Services;
using BLL;

namespace GeniusWebApplication
{
    public partial class result : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable ht = (Hashtable)HttpContext.Current.Session["researHT"];
            if (ht == null)
            {
                Response.Redirect("index.aspx");
                return;
            }
            //取出搜索类型
            string isFulltext = (string)ht["isFulltext"];
            string searchText = (string)ht["searchText"];
            ph_searchResult.Controls.Add(new LiteralControl("<h2><span class='highlight'>" + searchText + "</span> 搜索结果</h2>"));
            if (isFulltext == "0") //普通搜索
            {
                IList<Resource> resourceList = (List<Resource>)ht["resourceList"];
                string resourceName = "";
                string description = "";
                if (resourceList.Count > 0) //输出列表
                {
                    foreach (Resource r in resourceList)
                    {
                        if (r.status == 1)
                        {
                            resourceName = r.resourceName;
                            description = r.description;
                            resourceName = resourceName.Replace(searchText, "<span class='hightlight'>" + searchText + "</span>");
                            description = description.Replace(searchText, "<span class='hightlight'>" + searchText + "</span>");

                            Panel p_item = new Panel();
                            if (r.type == 2)
                            {
                                p_item.Controls.Add(new LiteralControl("<li><a href='./item.aspx?resourceId=" + r.id + "'><h3>" + resourceName + "</h3></a><br/><h7>" + description + "</h7><br/><h5>" + r.uploadTime + " 上传者：" + r.uploader.name + "</h5></li>"));
                            }
                            else if (r.type == 1)
                            {
                                p_item.Controls.Add(new LiteralControl("<li><a href='./item_media.aspx?resourceId=" + r.id + "'><h3>" + resourceName + "</h3></a><br/><h7>" + description + "</h7><br/><h5>" + r.uploadTime + " 上传者：" + r.uploader.name + "</h5></li>"));

                            }
                            else if (r.type == 3)
                            {
                                p_item.Controls.Add(new LiteralControl("<li><a href='./item_else.aspx?resourceId=" + r.id + "'><h3>" + resourceName + "</h3></a><br/><h7>" + description + "</h7><br/><h5>" + r.uploadTime + " 上传者：" + r.uploader.name + "</h5></li>"));

                            }
                            ph_searchResult.Controls.Add(p_item);
                        }
                    }
                }
                else //输出无结果
                {
                    ph_searchResult.Controls.Add(new LiteralControl("无结果"));
                }

            }
            else //高级搜索
            {
                IList<Resource> resourceList = (List<Resource>)ht["resourceList"];
                IList<string> contentList = (List<string>)ht["contentList"];

                string resourceName = "";
                string description = "";
                string content = "";

                if ((string)ht["result"] == "1") //输出列表
                {
                    int i = 0;
                    foreach (Resource r in resourceList)
                    {
                        if (r.status == 1)
                        {
                            resourceName = r.resourceName;
                            description = r.description;
                            resourceName = resourceName.Replace(searchText, "<span class='highlight'>" + searchText + "</span>");
                            description = description.Replace(searchText, "<span class='highlight'>" + searchText + "</span>");
                            content = contentList[i];
                            Panel p_item = new Panel();

                            if (r.type == 2)
                            {
                                p_item.Controls.Add(new LiteralControl("<li><a href='./item.aspx?resourceId=" + r.id + "'><h3>" + resourceName + "</h3></a><br/><h7>" + description + "<br/><br/>" + content + "</h7><br/><h5>" + r.uploadTime + " 上传者：" + r.uploader.name + "</h5></li>"));
                            }
                            else if (r.type == 1)
                            {
                                p_item.Controls.Add(new LiteralControl("<li><a href='./item_media.aspx?resourceId=" + r.id + "'><h3>" + resourceName + "</h3></a><br/><h7>" + description + "<br/><br/>" + content + "</h7><br/><h5>" + r.uploadTime + " 上传者：" + r.uploader.name + "</h5></li>"));

                            }
                            else if (r.type == 3)
                            {
                                p_item.Controls.Add(new LiteralControl("<li><a href='./item_else.aspx?resourceId=" + r.id + "'><h3>" + resourceName + "</h3></a><br/><h7>" + description + "<br/><br/>" + content + "</h7><br/><h5>" + r.uploadTime + " 上传者：" + r.uploader.name + "</h5></li>"));

                            }

                            ph_searchResult.Controls.Add(p_item);
                            i++;
                        }
                    }
                }
                else //输出无结果
                {
                    ph_searchResult.Controls.Add(new LiteralControl("无结果"));
                }
            }

            //初始化关键字
            IList<Keyword> kl = new KeywordService().getKeywords();
            List<string> keyStringList = new List<string>();
            List<int> keyStringListBig = new List<int>();
            foreach (Keyword k in kl)
            {

                if (keyStringList.Count() == 0)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else if (keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; }) == -1)
                {
                    keyStringList.Add(k.keywordName);
                    keyStringListBig.Add(5);
                }
                else
                {
                    int ii = keyStringList.FindIndex(delegate(string s) { return s == k.keywordName; });
                    if (keyStringListBig[ii] >= 2)
                    {
                        keyStringListBig[ii] = keyStringListBig[ii] - 1;
                    }
                }
            }

            for (int m = 0; m < keyStringList.Count(); m++)
            {
                this.keys.InnerHtml = this.keys.InnerHtml + "<div class='keysItem'><p class='ggz" + keyStringListBig[m] + "'><a href='./keywords.aspx?keys=" + keyStringList[m] + "'>" + keyStringList[m] + "</a></p></div>";
            }

        }

    }
}